package cn.com.joyplus.wechat.service;

import cn.com.joyplus.wechat.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by 王灏川(wangrs@qq.com) on 2017/8/11 0011.
 */
@Service
@SuppressWarnings("unchecked")
public class WechatCommonApiService {

    @Autowired
    RemoteResourceService remoteResourceService;

    @Autowired
    WechatCoreApiService wechatCoreApiService;


    private Map<String, String> commonApi = new HashMap<String, String>() {{
        //创建自定义菜单
        put("createMenu", String.format("%s s", Constant.WECHAT_API_BASE_URL));
        //获取自定义菜单
        put("getMenu", String.format("%s/cgi-bin/menu/get?access_token=%%s", Constant.WECHAT_API_BASE_URL));
        //删除自定义菜单
        put("cleanMenu", String.format("%s/cgi-bin/menu/delete?access_token=%%s", Constant.WECHAT_API_BASE_URL));
        //获取关注用户OpenId
        put("getAllUserOpenids", String.format("%s/cgi-bin/user/get?access_token=%%s", Constant.WECHAT_API_BASE_URL));
        //批量获取用户详情
        put("getAllUserDetail", String.format("%s/cgi-bin/user/info/batchget?access_token=%%s", Constant.WECHAT_API_BASE_URL));
        //批量获取用户详情
        put("getAllUserDetail", String.format("%s/cgi-bin/user/info/batchget?access_token=%%s", Constant.WECHAT_API_BASE_URL));
        //发送模板消息
        put("sendTemplateMsg", String.format("%s/cgi-bin/message/template/send?access_token=%%s", Constant.WECHAT_API_BASE_URL));


    }};


    public String callCommonApi(String code, Object params) throws Exception {
        return remoteResourceService.getRemoteResource(
                String.format(commonApi.get(code), wechatCoreApiService.getWechatAccessToken()),
                params, "POST");
    }

    public String callCommonApi(String code) throws Exception {
        return remoteResourceService.getRemoteResource(String.format(commonApi.get(code), wechatCoreApiService.getWechatAccessToken()));
    }

    public String getApiUrl(String code){
        return String.format(commonApi.get(code), wechatCoreApiService.getWechatAccessToken());
    }

}
